﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using LINQPad.OpenAccess.Helpers;
using LINQPad.OpenAccess.UI.Services;
using LINQPad.OpenAccess.UI.ViewModel;

namespace LINQPad.OpenAccess.UI.View
{
    /// <summary>
    /// ConnectionSettingsViewHost - static members
    /// </summary>
    partial class ConnectionSettingsViewHost
    {
        #region Private

        #region - Vars

        private static readonly DependencyPropertyKey _ViewPropertyKey;

        #endregion

        #region - Constructor

        /// <summary>
        /// Initializes the <see cref="ConnectionSettingsViewHost" /> class.
        /// </summary>
        static ConnectionSettingsViewHost( )
        {
            DefaultStyleKeyProperty.OverrideMetadata( typeof( ConnectionSettingsViewHost ), new FrameworkPropertyMetadata( typeof( ConnectionSettingsViewHost ) ) );

            ConnectionSettingsProperty = DependencyProperty.Register( "ConnectionSettings", typeof( IConnectionSettingsViewModel ), typeof( ConnectionSettingsViewHost ), new PropertyMetadata( DelegateTo<IConnectionSettingsViewModel>( e => e.OnConnectionSettingsChanged ) ) );
            ViewFactoryProperty = DependencyProperty.Register( "ViewFactory", typeof( IConnectionSettingsViewFactory ), typeof( ConnectionSettingsViewHost ), new PropertyMetadata( DelegateTo<IConnectionSettingsViewFactory>( e => e.OnViewFactoryChanged ) ) );
            _ViewPropertyKey = DependencyProperty.RegisterReadOnly( "View", typeof( FrameworkElement ), typeof( ConnectionSettingsViewHost ), new PropertyMetadata( DelegateTo<FrameworkElement>( e => e.OnViewChanged ) ) );
            ViewProperty = _ViewPropertyKey.DependencyProperty;
        }

        #endregion

        #region - Functions

        /// <summary>
        /// Used to delegate the property changed event to the corresponding handler.
        /// </summary>
        /// <typeparam name="T">The type of the property.</typeparam>
        /// <param name="onPropertyChangedSelector">The selector for the OnPropertyChanged delegate.</param>
        /// <returns>The event handler.</returns>
        private static PropertyChangedCallback DelegateTo<T>( Func<ConnectionSettingsViewHost, Action<T, T>> onPropertyChangedSelector )
        {
            Guard.ArgumentNotNull( onPropertyChangedSelector, "onPropertyChangedSelector" );
            return ( d, e ) =>
            {
                var host = d as ConnectionSettingsViewHost;
                if ( host != null )
                    onPropertyChangedSelector( host )( ( T ) e.NewValue, ( T ) e.OldValue );
            };
        }

        #endregion

        #endregion

        #region Public

        #region - Dependency properties

        /// <summary>
        /// The connection settings dependency property.
        /// </summary>
        public static readonly DependencyProperty ConnectionSettingsProperty;
        /// <summary>
        /// The view factory dependency property.
        /// </summary>
        public static readonly DependencyProperty ViewFactoryProperty;
        /// <summary>
        /// The view dependency property.
        /// </summary>
        public static readonly DependencyProperty ViewProperty;

        #endregion

        #endregion
    }
}
